import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:changxindianzishangwu/util/colors.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:path/path.dart';

import 'app_color.dart';

class UniversalView {
  Widget line(
      {Color color = const Color.fromRGBO(232, 232, 232, 1),
      double indent = 0.0,
      double endIndent = 0.0}) {
    return Divider(
      height: 1,
      color: color,
      indent: width(indent),
      endIndent: width(endIndent),
      thickness: ScreenUtil().setHeight(1),
    );
  }

  Widget ImageClip({
    double width = 50,
    double height = 50,
    String path = "images/ic_default_image.jpg", //images图片
    String filePath = "", //手机文件途径地址
    String netWork = "",
    String placeholder = "images/ic_default_image.png",
  }) {
    return Container(
      width: width,
      height: height,
      child: ClipRRect(
        borderRadius: BorderRadius.circular(10),
        child: filePath.isNotEmpty
            ? Image.file(
                File(filePath),
                fit: BoxFit.cover,
              )
            : netWork.isNotEmpty
                ? FadeInImage.assetNetwork(
                    placeholder: placeholder,
                    image: netWork,
                    fit: BoxFit.cover,
                  )
                : Image.asset(path, fit: BoxFit.cover),
      ),
    );
  }

  Widget longBtn(String title, Function function, {double widthSize = 300}) {
    return Center(
      child: TextButton(
        onPressed: function,
        child: Text(
          title,
        ),
        style: ButtonStyle(
          textStyle: MaterialStateProperty.all(
            TextStyle(
              fontSize: ScreenUtil().setSp(15),
              color: Colors.white,
            ),
          ),
          backgroundColor: MaterialStateProperty.all(AppColor.colorTheme),
          padding: MaterialStateProperty.all(EdgeInsets.all(10)),
          minimumSize:
              MaterialStateProperty.all(Size(width(widthSize), height(44))),
        ),
      ),
    );
  }

  Widget longBtnLine(String title, Function function,
      {double widthSize = 300}) {
    return Center(
      child: TextButton(
        onPressed: function,
        child: Text(title,
            style: TextStyle(
              fontSize: ScreenUtil().setSp(15),
              color: AppColor.colorTheme,
            )),
        style: OutlinedButton.styleFrom(
            side: BorderSide(color: AppColor.colorTheme),
            minimumSize: Size(width(widthSize), height(44))),
      ),
    );
  }

  ///用例一则
// void _selectAction() async {
//   int index1 = await showPayActionSheets(context: context, title: '清楚聊天记录');
//   print(index1);
//   Navigator.of(context).pop();
// }
//
// /// 具体使用方式
// Future<int> showPayActionSheets({
//   @required BuildContext context,
//   @required String title,
// }) {
//   return showCustomBottomSheet(context: context, title: title, children: [
//     actionItem(context: context, index: 1, title: '清楚15天聊天记录'),
//     actionItem(context: context, index: 2, title: '清楚30天聊天记录'),
//     actionItem(
//         context: context, index: 2, title: '清楚60天聊天记录', isLastOne: true),
//   ]);
// }

  CupertinoAlertDialog getDialog(context, title, content,
      {String left = "取消", String right = "确定"}) {
    return CupertinoAlertDialog(
        content: new SingleChildScrollView(
          child: ListBody(
            children: <Widget>[
              Text(title),
              Text(content),
            ],
          ),
        ),
        actions: <Widget>[
          CupertinoDialogAction(
            child: Text(
              left,
              style: TextStyle(fontSize: sp(17), color: AppColor.colorGrey153),
            ),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
          CupertinoDialogAction(
            child: Text(
              right,
              style: TextStyle(fontSize: sp(17), color: AppColor.colorTheme),
            ),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
        ]);
  }
}

class OverScrollBehavior extends ScrollBehavior {
  @override
  Widget buildViewportChrome(
      BuildContext context, Widget child, AxisDirection axisDirection) {
    return GlowingOverscrollIndicator(
      child: child,
      //不显示头部水波纹
      showLeading: false,
      //不显示尾部水波纹
      showTrailing: false,
      axisDirection: axisDirection,
      color: Theme.of(context).accentColor,
    );
  }
}

///有缓存的圆角头像
class HeadView extends CachedNetworkImage {
  HeadView(
    String imageUrl, //图片地址
    double height,
    double width, {
    final imageBuilder,
  }) : super(
          placeholder: (context, url) {
            return Container(child: Image.asset("images/ic_default_photo.png"));
          },
          imageUrl: imageUrl,
          height: height,
          width: width,
          imageBuilder: imageBuilder == null
              ? (context, imageProvider) => Container(
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(5),
                      image: DecorationImage(
                        image: imageProvider,
                        fit: BoxFit.fill,
                      ),
                    ),
                  )
              : imageBuilder,
          errorWidget: (context, url, error) =>
              Container(child: Image.asset("images/ic_default_photo.png")),
        );
}
